function g = gradient_rosenbrock(x)
    d = size(x, 1);
    if (d == 1)
        g = 2.0 * (x - 1.0);
    else
        g = zeros(size(x));
        g(1, :) = 400.0 * x(1, :) .* (x(1, :).^2 - x(2, :)) + 2.0 * (x(1, :) - 1.0);
        g(2:end - 1,:) = 200.0 * (x(2:end - 1, :) - x(1:end - 2, :).^2) + ...
                         400.0 * (x(2:end - 1, :).^2 - x(3:end, :)) .* x(2:end - 1, :) + ...
                         2.0 * (x(2:end - 1, :) - 1.0);
        g(end, :) = 200.0 * (x(end, :) - x(end - 1, :).^2);
    end
end